//模块基类
Ext.namespace('app');

app.module = function(tab){
    this.main = tab;
    this.init();
}

Ext.extend(app.module, Ext.util.Observable, {
    init : Ext.emptyFn
});

//主程序类
app.exam = function(){
    this.init();
}

Ext.extend(app.exam, Ext.util.Observable, {
    init: function(){
        var loader = new Ext.tree.TreeLoader({dataUrl:'user/menus.do'});
        loader.processResponse = function(response, node, callback){
            var json = response.responseText;
            try{
                var json = eval("(" + json + ")");
                node.beginUpdate();
                var o = json["key"];
                for(var i=0,len=o.length; i<len; i++){
                    var n = this.createNode(o[i]);
                    if(n){
                        node.appendChild(n);
                    }
                }
                node.endUpdate();
                if(typeof callback == "function"){
                    callback(this,node);
                }
            } catch(e) {
                this.handleFailure(response);
            }
        }
        this.tree = new Ext.tree.TreePanel({
            id: 'treeMenu',
            xtype: 'treepanel',
            width: 190,
            collapsible: true,
            title: '系统管理',
            region: 'west',
            split: true,
            root: {
                id: 0,
                text: '根节点',
                expanded: true
            },
            loader: loader,
            lines:false,        // 去掉树的线
            rootVisible : false,// 根节点是否可见
            tools:[{
                id:'refresh',
                handler:function(){
                    var tree = Ext.getCmp('treeMenu');
                    tree.root.reload();
                }
            }]
        });
        this.tree.on('click',this.clickTree, this);

        var viewport = new Ext.Viewport({
            layout: 'border',
            items: [
                {
                    id: 'maintabs',
                    xtype: 'tabpanel',
                    title: '在线考试系统',
                    margins:'0 5 0 0',
                    activeTab: 0,
                    autoScroll: true,
                    region: 'center',
                    items: [
                        {
                            xtype: 'panel',
                            title: '首页'
                        }
                    ]
                },
                this.tree,
                {
                    xtype: 'panel',
                    collapsible: true,
                    frame: true,
                    title: '在线考试系统',
                    region: 'north',
                    contentEl: 'north'
                },
                {
                    xtype: 'box',
                    height: 17,
                    region: 'south',
                    contentEl: 'south'
                }
            ]
        });
        this.body = Ext.getCmp('maintabs');
        this.body.on('tabchange', this.changeTab, this);
        this.loadMask = new Ext.LoadMask(this.body.body);
    },
    //切换TabPanel标签方法
    changeTab: function(p,t){
        //如果存在相应树节点，就选中,否则就清空选择状态
        var id = t.id.replace('tab-','');
        var node = this.tree.getNodeById(id);
        if(node){
            this.tree.getSelectionModel().select(node);
        }else{
            this.tree.getSelectionModel().clearSelections();
        }
    },

    //点击菜单方法(node:被点击的节点)
    clickTree: function(node){
        if(!node.leaf){
            return false;
        }
        var id = 'tab-' + node.id;
        var tab = Ext.getCmp(id);
        if(!tab){
            tab = this.body.add(new Ext.Panel({
                id: id,
                title: node.text,
                layout: 'fit',
                closable: true
            }));
            this.body.setActiveTab(tab);
            //加载模块
            this.loadModel(node.attributes.url,tab);
        }else{
            this.body.setActiveTab(tab);
        }
    },

    //加载模块方法(id:模块ID;tab:模块显示在哪里)
    loadModel: function(id,tab){
        //定义模块变量
        var model;
        if(this[id]){
            //如果模块类已存在，就直接实例化
            model = new this[id](tab);
        }else{
            this.loadMask.show();
            Ext.Ajax.request({
                method:'GET',
                url: 'module/'+id+'.js',
                scope: this,
                success: function(response){
                    //获取模块类  
                    this[id] = eval(response.responseText);
                    //实例化模块类
                    model = new this[id](tab);
                    this.loadMask.hide();
                }
            });
        }
    }
});

//实例化主程序类
Ext.BLANK_IMAGE_URL = 'js/ext/resources/images/default/s.gif';
Ext.form.Field.prototype.msgTarget = 'title';
Ext.onReady(function(){
    //初始化Ext.QuickTips，以使得tip提示可用
    Ext.QuickTips.init();
    myApp = new app.exam();
});